Skip to content

refactor(relation): route first/last through the execute seam - #42

Open
dex-the-ai wants to merge 1 commit into
masterfrom
architecture-audit/couchbase-ruby-orm-2026w48
Open

dex-the-ai wants to merge 1 commit into
masterfrom
architecture-audit/couchbase-ruby-orm-2026w48

Conversation

@dex-the-ai

Copy link
Copy Markdown

Summary

CouchbaseOrm_Relation#execute is meant to be the one place where a relation statement reaches the cluster. It applies the query options (N1ql.config merged with #with(...)) and writes the debug log. #first and #last didn't use it. Each called @model.cluster.query(..., build_query_options(...)) itself. That left three copies of the same logic, and first/last never wrote the Relation query: debug line.

This PR:

  • first / last now go through ids → query → execute, like count, to_a, pluck, delete_all and update_all. Behaviour is unchanged: first still adds limit 1, and both still return nil when nothing matches.
  • to_n1ql / to_n1ql_with_params share one private build_select(params), so the select raw meta().id … where … order by … limit shape is written once. With params = nil, to_n1ql still inlines values for display.

Net change in lib/: −4 lines. No public API change.

Why this seam

The adhoc / prepared-statement work (#36) added query_options and #with to the relation. Any later option or logging change should touch only execute and build_query_options. Before this PR, first and last would have to be updated separately, and a missed update would be silent.

Tests

New query execution seam block in spec/relation_spec.rb:

  • For each of ids, first, last, count, to_a and update_all, it checks that exactly one statement goes through #execute and gets the relation's options (adhoc: false, client_context_id, default scan_consistency: :request_plus).
  • It checks that first and last return the right records.
  • It checks that to_n1ql and to_n1ql_with_params build the same statement, with only the values bound differently.

Against the old lib/, the first and last seam cases fail and every other case passes. On this branch all of them pass, 5 out of 5 repeated runs.

Validation

Ran locally against Couchbase Server Enterprise 7.6.12 in Docker, set up the same way as ci/run_couchbase.sh (default bucket, tester user, type/rating/name indexes), on Ruby 3.3.8:

ActiveModel Result Line coverage
8.0.2 405 examples, 0 failures 95.33%
7.2.0 405 examples, 0 failures 95.33%

Baseline on master before the change: 397 examples, 0 failures.

🤖 Generated with Claude Code

Relation#first and #last called cluster.query and build_query_options
directly instead of going through #execute, so the query-option merge
(N1ql.config + #with) and the debug logging lived in three places.
They now go through #ids -> #query -> #execute, like every other
terminal method.

to_n1ql and to_n1ql_with_params now share one private build_select,
so the SELECT shape is defined once.

Add a spec that checks ids/first/last/count/to_a/update_all each run
exactly one statement through #execute with the relation's options.
The first/last cases fail on the old code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dex-the-ai
dex-the-ai requested review from pimpin and prajwal-pai77 and a balanced review from Copilot November 23, 2026 02:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The relation-query execution refactor has no active review issues blocking approval.

No open issues 🎉

What changed in this PR

This PR centralizes relation query execution so first and last use the same query-option handling and debug logging as other terminal methods, building on the prepared-statement support from #36.

Changes:

  • Route first and last through ids → query → execute, preserving existing selection behavior.
  • Share SELECT statement construction through a private build_select helper.
  • Add specs for execution routing, query options, record selection, and inline versus parameterized statements.
File Description
spec/​relation_spec.rb Adds execution-path, query-option, and statement-equivalence coverage.
lib/​couchbase-orm/​relation.rb Centralizes first/last execution and deduplicates SELECT construction.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants